﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using XMK.Tadong.Biz;
using XMK.Framework.Model;
using XMK.Utils;

using Aspose.Cells;

namespace XMK.Tadong.Web.Handlers
{
    public partial class LogHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = string.Empty;
            try
            {
                string op = Request.Params.Get("op");
                
                switch (op)
                {
                    case "search":
                        string logType = Request.Params.Get("logType");
                        string startDate = Request.Params.Get("startDate");
                        string endDate = Request.Params.Get("endDate");
                        string userId = Request.Params.Get("uid").Replace("\'","");
                        string txtKeyw = Request.Params.Get("txtKeyw").Replace("\'", "").Replace("%", "");
                        string pageIndex = Request.Params.Get("page");
                        string rows = Request.Params.Get("rows"); 

                        PageInfo page = new PageInfo();
                        page.CurrentPage = int.Parse(pageIndex);
                        page.PageSize = int.Parse(rows);
                        ResponseInfo resp = XMKLogBiz.QueryLog(page, 
                            ParseHelper.ParseEnum<Model.LogOperateType>(logType, Model.LogOperateType.Search),
                            userId, DateTime.Parse(startDate), DateTime.Parse(endDate).AddDays(1), txtKeyw);
                        result = string.Concat("{\"total\":",
                            resp.Page.Records, ", \"rows\":", JsonHelper.ToJson(resp.Data), "}");
                        break;
                    case "delete":
                        result = btnClearLog_Click();
                        break;
                }
                
            }
            catch (System.Exception ex)
            {
                result = string.Concat("{\"total\":",
                    0, ", \"rows\":[]}");
                LogHelper.WriteLog("Abnormal," + ex.ToString());
            }
            
            Response.ContentType = "text/json";
            Response.Write(result);
            Response.End();
        }

        /// <summary>
        /// 清空日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected string btnClearLog_Click()
        {
            string logType = Request.Params.Get("logType");
            string startDate = Request.Params.Get("startDate");
            string endDate = Request.Params.Get("endDate");
            string userId = Request.Params.Get("uid");
            string keyw = Request.Params.Get("txtKeyw");

            // 导出
            ResponseInfo exportRes = XMKLogBiz.DeleteLog(
                ParseHelper.ParseEnum<Model.LogOperateType>(logType, Model.LogOperateType.Search),
                userId, DateTime.Parse(startDate), DateTime.Parse(endDate).AddDays(1),
                keyw.Replace("'", "").Replace("%", ""));

            if (string.IsNullOrEmpty(exportRes.ErrMsg))
                return "{\"success\":true}";
            return "{\"success\":false}";
        }
    }
}